import { Injectable } from '@angular/core';
import * as gc from 'src/app/global/const';
import { HttpClient } from '@angular/common/http';
import { JsonResult, PrimeNgTableCol, ZorroOption } from 'src/app/global/model';
import { Observable } from 'rxjs';

@Injectable({
  providedIn: 'root'
})
export class TablestyleService {

  path_pre = 'tableStyle';

  url = {
    'selectAll':gc.BACKEND_SERVER_ROOT + '/' + this.path_pre + '/a',
    'selectTable':gc.BACKEND_SERVER_ROOT + '/' + this.path_pre + '/table',
    'saveOne':gc.BACKEND_SERVER_ROOT + '/' + this.path_pre,
    'deleteByPrimaryKey':gc.BACKEND_SERVER_ROOT + '/' + this.path_pre
  }

  constructor(
    private http:HttpClient
  ) { }

  // 保存 - 新增或者修改
  saveOne(record:TableStyle):Observable<JsonResult>{
    return this.http.post<JsonResult>(this.url.saveOne,record);
  }

  deleteByPrimaryKey(iid:number):Observable<JsonResult>{
    const url = this.url.deleteByPrimaryKey + '/' + iid;
    return this.http.delete<JsonResult>(url);
  }

  selectAll():Observable<JsonResult>{
    return this.http.get<JsonResult>(this.url.selectAll);
  }

  selectTable(tableName:string):Observable<JsonResult>{
    const url = this.url.selectTable + '/' + tableName;
    return this.http.get<JsonResult>(url);
  }

  // 将 TableStyle 类型的数组转换为 PrimeNgTableCol 类型的数组
  toPrimeNgTableCols(array:TableStyle[]):PrimeNgTableCol[]{
    let cols:PrimeNgTableCol[] = [];
    if( array.length<=0 ) return cols;

    array.forEach(ele => {
      cols.push({'header':ele.text,'field':ele.field_name,'width':ele.width_primeng_str});
    });

    return cols;
  }

  toZorroOptions(array:TableStyle[]):ZorroOption[]{
    let opts:ZorroOption[] = [];
    if( array.length<=0 ) return opts;
    array.forEach(ele => {
      opts.push({'label':ele.text,'value':ele.field_name});
    });

    return opts;
  }
}


// Author：chanchaw
// Generator Version：1.0 - 2019年11月16日 16点46分
// This Model Created：Tue Dec 10 2019 15:45:08 GMT+0800 (中国标准时间)
export class TableStyle{
  align_cell:string;
  align_header:string;
  create_time:Date;
  field_name:string;
  iid:number;
  immutable:boolean;
  seq:number;
  table_name:string;
  text:string;
  visible:boolean;
  width_primeng:number;
  width_zorro:number;
  tick_pipe:boolean;
  width_primeng_str:string;
}
